package com.hitqz.robot.biz.flow.component.patrol;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.dto.PatrolResultExcelDto;
import com.hitqz.robot.api.business.entity.PatrolResultEntity;
import com.hitqz.robot.api.business.enums.ResultTypeEnum;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolTicketsDto;
import com.hitqz.robot.api.business.params.QueryPatrolSummaryParams;
import com.hitqz.robot.api.business.util.ExcelUtil;
import com.hitqz.robot.api.business.vo.PatrolResultSummaryVo;
import com.hitqz.robot.api.common.entity.*;
import com.hitqz.robot.biz.core.FileProperties;
import com.hitqz.robot.biz.service.*;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

@Slf4j
@LiteflowComponent(value = "GenerateExcel", name = "[巡检]输出excel")
@AllArgsConstructor
public class GenerateExcel extends NodeComponent {

    private final PatrolObjectService patrolObjectService;

    private final PatrolResultService patrolResultService;


    private final FileProperties fileProperties;

    private final ExecutorService customThreadPool;

    @Override
    public void process() throws Exception {
        FlowContextDto dto = this.getRequestData();
        Runnable runnableTask = () -> {
            Long ticketsId = dto.getTickets().getId();
            QueryPatrolSummaryParams query = new QueryPatrolSummaryParams();
            query.setTicketsId(ticketsId.intValue());
            PatrolResultSummaryVo summary = patrolResultService.summary(query);
            Map<Long, String> objectMap = patrolObjectService.list().stream().collect(Collectors.toMap(PatrolObject::getId, PatrolObject::getName));
            List<PatrolResultEntity> data = patrolResultService.list(Wrappers.<PatrolResultEntity>lambdaQuery().eq(PatrolResultEntity::getTicketsId, ticketsId));
            List<PatrolResultExcelDto> list = new ArrayList<>();
            //属性替换
            for (int i = 0; i < data.size(); i++) {
                PatrolResultEntity patrolResult = data.get(i);
                PatrolResultExcelDto excelDto = BeanUtil.copyProperties(patrolResult, PatrolResultExcelDto.class);
                excelDto.setIndex(i+1);//序号
                String url = StrUtil.isBlank(patrolResult.getVisionMediaUrl()) ? patrolResult.getMediaUrl() : patrolResult.getVisionMediaUrl();
                excelDto.setPath(fileProperties.getLocal().getBasePath()+ File.separator+url);//文件采用相对路径，所以要加前缀
                excelDto.setObjectName(objectMap.getOrDefault(patrolResult.getPatrolObjectId().longValue(),""));
                list.add(excelDto);
            }
            //动态表头
            List<List<String>> headTitles = ExcelUtil.head(summary);
            //写文件
            String patrolExcelFilePath = ExcelUtil.getPatrolExcelFilePath(fileProperties.getLocal().getBasePath(), ticketsId.intValue());
            log.info("输出excel:{}",patrolExcelFilePath);
            EasyExcel.write(patrolExcelFilePath, PatrolResultExcelDto.class)
                    // 这里放入动态头
                    .head(headTitles)
                    .sheet("Sheet1")
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                    // 当然这里数据也可以用 List<List<String>> 去传入
                    .doWrite(list);
        };
        customThreadPool.submit(runnableTask);
    }

}